home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / rbsetnv1.zip / DUP.C < prev    next >
Text File  |  1990-01-27  |  1KB  |  47 lines

  1. /*
  2.  * For some reason I have not figured out, TurboC v2.0 dup() and dup2() functions
  3.  * will not let me save and reassign stdin, needed for the write pipe, but they
  4.  * will work ok for the read pipe. (freopen works on stdin, but I cannot restore
  5.  * it without dup2())
  6.  * Here are very simple versions that do the direct DOS calls and seem to work
  7.  *        R. Brittain (Tested with DOS 4.0, and SHARE installed)
  8.  */
  9. #include <dos.h>
  10. #include <errno.h>
  11.  
  12. dup ( int fd )
  13. {
  14. /*
  15.  * Call DOS function to create a new file handle as a duplicate of
  16.  * existing handle fd
  17.  */
  18.     union REGS regs;
  19.     regs.h.ah = 0x45;
  20.     regs.x.bx = fd;
  21.     intdos(®s,®s);
  22.     if (regs.x.cflag) {
  23.         errno = regs.x.ax;
  24.         return(-1);
  25.     } else {
  26.         return(regs.x.ax);
  27.     }
  28. }
  29.  
  30. dup2 (int ofd, int nfd )
  31. {
  32. /*
  33.  * Call DOS function to duplicate handle nfd onto existing handle ofd
  34.  */
  35.     union REGS regs;
  36.     regs.h.ah = 0x46;
  37.     regs.x.bx = nfd;
  38.     regs.x.cx = ofd;
  39.     intdos(®s,®s);
  40.     if (regs.x.cflag) {
  41.         errno = regs.x.ax;
  42.         return(-1);
  43.     } else {
  44.         return(0);
  45.     }
  46. }
  47.